/*
 * Static.h
 *
 *  Created on: 2014. 7. 17.
 *      Author: mega
 */

#ifndef GUILDSERVER_STATIC_H_
#define GUILDSERVER_STATIC_H_

#include <map>

#include <Toolkit/Single.h>
#include <Toolkit/Pool.h>

#include "Guild.h"
#include "Member.h"
#include "GuildRank.h"
#include "ChannelServer.h"





namespace GuildServer {

typedef Toolkit::Pool<Guild>	GUILD_POOL;
typedef Toolkit::Pool<Member>	MEMBER_POOL;

class ChannelServer;

class Static : public ::Toolkit::Single<Static>
{
public:
	typedef std::map<uint32_t, std::shared_ptr<Guild>>		GUILDMAP;
	typedef std::map<uint32_t, ChannelServer*>		CHANNELSERVERMAP;

public:
	Static();
	virtual ~Static();

	std::shared_ptr<Guild> LoadGuild( uint32_t guild_seq );
	bool		AddGuild(std::shared_ptr<Guild> pGuild);
	bool		DelGuild(uint32_t guildSeq);
	std::shared_ptr<Guild>	GetGuild(uint32_t guildSeq);

	std::shared_ptr<Guild>	NewGuild();
	std::shared_ptr<Member>	NewMember();

	void 	LoadRecommendedGuilds();
	//int 	GetGuildRank( int a_iStart );

	void	SetTest(bool bTest) { m_bTest = bTest; }
	bool	GetTest() { return m_bTest; }


	GUILDMAP &	GetGuildMap() { return m_GuildTree; }
	GuildRank& GetRankList() { return m_kRankList; }


	template <class MSG> void BroadcastMsgToChannelServerAll(const MSG& msg)
	{
		//LOG(DEV, __FUNCTION__, "> sender: ", msg.nickname.c_str(), ", chat: ", msg.chat.c_str());

		auto iter = m_ChannelServerTree.begin();
		for ( ; iter != m_ChannelServerTree.end(); ++iter )
		{
			ChannelServer* pServer = iter->second;
			if ( NULL == pServer )
				continue;

			//LOG(DEV, __FUNCTION__, "> \tTo channel server> session key: ", pServer->GetSessionKey(), ", channel key: ", pServer->GetChannelKey());
			pServer->SendMsg( msg );
		}
	}

	void BroadcastChatMsgToChannelServerAll(const BroadcastGuildChatAck& msg);


	void AddChannelServer( ChannelServer* a_pSession );
	void DelChannelServer( int a_iSessionKey );

private:
	GUILDMAP	m_GuildTree;

	GUILD_POOL	m_GuildPool;
	MEMBER_POOL	m_MemberPool;

	GuildRank m_kRankList;

	bool		m_bTest;


	CHANNELSERVERMAP m_ChannelServerTree;


};

} /*GuildServer*/


#endif /* GUILDSERVER_STATIC_H_ */
